<?php
declare (strict_types = 1);

namespace app\validate\common\user;

use app\model\communal\UserModel;
use think\Validate;

class CleaningValidate extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'id' => ['require', 'number', 'gt:0'],
        'mobile' => ['require', 'mobile'],
        'account' => ['require', 'max:50'],
        'password' => ['require'],
        'type' => ['require', 'in:1,2'],

        /*
         * info
         */
        'company_id' => ['number', 'gt:0'],

        /*
         * 导入
         */
        'file' => ['require'], // 10M

        'state' => ['in:1,2'],
        'page' => ['number', 'gt:0'],
        'page_size' => ['number', 'gt:0'],
    ];

    protected $field = [
        'name' => '保洁名称',
        'mobile' => '手机号码',
        'account' => '账号',
        'password' => '密码',

        /*
         * info
         */
        'company_id' => '隶属公司',

        /*
         * 导入
         */
        'file' => '文件',

        'page' => '当前页数',
        'page_size' => '每页条数',
        'orderBy' => '排序字段',
        'orderDir' => '排序规则',
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名' =>  '错误信息'
     *
     * @var array
     */
    protected $message = [
    ];

    /**
     * 场景
     * @var array
     */
    protected $scene = [
        'index' => ['page', 'page_size'],
        'code' => ['code'],
        'file' => ['file'],
    ];

    /**
     * scene save owner
     *
     * @return $this
     */
    public function sceneSave()
    {
        return $this->only(UserModel::$saveCleaningColumn)
            ->append('mobile', 'checkMobileUnique:save')
            ->append('account', 'checkAccountUnique:save');
    }

    /**
     * scene update
     *
     * @return $this
     */
    public function sceneUpdate()
    {
        return $this->only(UserModel::$updateCleaningColumn)
            ->remove('account', 'require')
            ->remove('password', 'require')
            ->append('mobile', 'checkMobileUnique:update')
            ->append('account', 'checkAccountUnique:update');
    }

    /**
     * scene item
     *
     * @return $this
     */
    public function sceneItem()
    {
        return $this->only(['id', 'state']);
    }

    /**
     * 唯一验证
     *
     * @param $value
     * @param $rule
     * @param $data
     * @return bool|mixed
     */
    protected function checkMobileUnique($value, $rule, $data)
    {
        $data['delete_at'] = 0;
        if ('save' === $rule) {
            $check = $this->unique($value, UserModel::class . ',mobile^delete_at', $data, 'mobile^delete_at');
        } else {
            $check = $this->unique($value,UserModel::class . ',mobile^delete_at,' . $data['id'], $data, 'mobile^delete_at');
        }

        if (true !== $check) {
            return lang(':attribute has exists');
        }

        return true;
    }

    /**
     * 唯一验证
     *
     * @param $value
     * @param $rule
     * @param $data
     * @return bool|mixed
     */
    protected function checkAccountUnique($value, $rule, $data)
    {
        $data['delete_at'] = 0;
        if ('save' === $rule) {
            $check = $this->unique($value, UserModel::class . ',account^delete_at', $data, 'account^delete_at');
        } else {
            $check = $this->unique($value,UserModel::class . ',account^delete_at,' . $data['id'], $data, 'account^delete_at');
        }

        if (true !== $check) {
            return lang(':attribute has exists');
        }

        return true;
    }
}
